﻿@{
    Layout = "";
}
<style>
    .ngdialog.ngdialog-theme-plain .ngdialog-content {
        width: 45% !important;
    }
</style>

<h3>积分商品管理</h3>

<div class="panel panel-default" ng-controller="ExchangeManageController" ng-class="{ 'whirl standard': isLoading,'': isLoading }">
    <div class="panel-heading">
        <div class="form-inline ng-pristine ng-valid">
            <div class="form-group" style="line-height: 50px;width:100%;">
                <input style="width: 15%;" placeholder="关键字" class="form-control" type="text" ng-model="Keywords">
                <select ng-model="IsDisplay" class="form-control">
                    <option value="">请选择销售状态</option>
                    <option value="1">销售中</option>
                    <option value="2">已下架</option>
                </select>

                <button type="submit" class="btn btn-labeled btn-primary" ng-click="tableParams.$params.page = 1;tableParams.reload()">
                    <span class="btn-label">
                        <em class="fa fa-search"></em>
                    </span>搜索
                </button>
                <button type="submit" class="btn btn-labeled btn-success" ng-click="updateNormalSaleProduct(0)">
                    <span class="btn-label">
                        <em class="fa fa-plus"></em>
                    </span>新增
                </button>
            </div>
        </div>
    </div>
    <div class="panel-body">
        <div class="table-responsive">
            <table ng-table="tableParams" class="table table-striped table-bproducted table-hover">
                <colgroup>
                    <col style="width: 10%" />
                    <col style="width: 15%" />
                    <col style="width: 10%" />
                    <col style="width: 10%" />
                    <col style="width: 5%" />
                    <col style="width: 5%" />
                    <col style="width: 5%" />
                    <col style="width: 5%" />
                    <col style="width: 5%" />
                    <col style="width: 5%" />
                    <col style="width: 5%" />
                    <col style="width: 5%" />
                    <col style="width: 5%" />
                    <col style="width: 10%" />
                </colgroup>
                <tbody>

                    <tr ng-repeat="NormalSaleProduct in $data">
                        <td data-title="'主图'" class="text-center">
                            <img ng-src="{{ ImgHostUrl + NormalSaleProduct.ImgUrl}}" style="max-width: 100px; max-height: 100px" onerror="javascript: this.src = '/Content/img/default.png';" />
                        </td>
                        <td data-title="'名称'" class="text-left">{{NormalSaleProduct.Name}}</td>
                        <td data-title="'编号'" class="text-left">{{NormalSaleProduct.BarCode}}</td>
                        <td data-title="'库存'" sortable="'TotalStock'" class="text-center">{{NormalSaleProduct.Stock}}</td>
                        <td data-title="'价格'" class="text-center">{{NormalSaleProduct.MarketPrice.toFixed(2)}}</td>
                        <td data-title="'积分'" class="text-center">{{NormalSaleProduct.Points}}</td>
                        <td data-title="'添加时间'" sortable="'AddTime'" class="text-center">{{NormalSaleProduct.AddTime}}</td>
                        <td data-title="'销售状态'" class="text-left">{{NormalSaleProduct.IsDisplay ? '销售中' : '已下架'}}</td>
                        <td data-title="'排序'" sortable="'Sort'" class="text-center">{{NormalSaleProduct.Sort}}</td>
                        <td data-title="''" nowrap="nowrap">
                            <button type="button" ng-click="updateNormalSaleProduct(NormalSaleProduct.Id)" class="btn btn-sm btn-edit" title="编辑">
                                <em class="fa fa-edit"></em>
                            </button>
                            <button type="button" ng-click="ChangeDisplay(NormalSaleProduct)" class="btn btn-sm btn-edit" title="{{NormalSaleProduct.IsDisplay ? '下架' : '上架'}}">
                                <em ng-class="{ 'fa fa-level-up': !NormalSaleProduct.IsDisplay ,'fa fa-level-down': NormalSaleProduct.IsDisplay }"></em>
                            </button>
                            @*<button type="button" ng-click="lookProduct(NormalSaleProduct.Sku)" class="btn btn-sm btn-edit" title="商品预览">
                                    <em class="fa fa-toggle-right"></em>
                                </button>*@
                            <button ng-show="!NormalSaleProduct.IsDisplay" type="button" ng-click="removeNormalSaleProduct($index,NormalSaleProduct)" class="btn btn-sm btn-danger" title="删除">
                                <em class="fa fa-trash-o"></em>
                            </button>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <script type="text/ng-template" id="modalDialogId">
        <div class="ngdialog-message text-center">
            <h3>确定要删除吗?</h3>
            <p>删除后不可恢复!</p>
            <br />
            <button type="button" ng-click="confirm()" class="btn btn-primary mr">确认</button>
            <button type="button" ng-click="closeThisDialog('button')" class="btn btn-default">取消</button>
        </div>
    </script>
    <script type="text/ng-template" id="confirmChangeStateDialogId">
        <div class="ngdialog-message text-center">
            <h3>确定进行上下架操作?</h3>
            <br />
            <button type="button" ng-click="confirm()" class="btn btn-primary mr">确认</button>
            <button type="button" ng-click="closeThisDialog('button')" class="btn btn-default">取消</button>
        </div>
    </script>
</div>
<script>
    App.controller('ExchangeManageController', [
    '$scope', '$resource', '$state', '$route', 'ngDialog', '$http', 'ngTableParams', 'ngTableDataService', 'Notify', '$modal', "$filter", "$timeout", "$stateParams", "$location",
    function ($scope, $resource, $state, $route, ngDialog, $http, ngTableParams, ngTableDataService, Notify, $modal, $filter, $timeout, $stateParams, $location) {
        'use strict';
        $scope.isLoading = true;
        $scope.PageDetailList = [];
        $scope.tableParams = new ngTableParams({
            page: 1,
            count: 10,
            sorting: {
                //Id: 'desc'     // 初始化排序
            }
        }, {
            getData: function ($defer, params) {
                $scope.isLoading = true;
                var requestParams = {
                    Skip: (params.page() - 1) * params.count(),
                    Take: params.count(),
                    Keywords: $scope.Keywords,
                    IsDisplayParm: $scope.IsDisplay
                };
                $http.post('/ProductExchange/ExchangeManage', ngTableDataService.getServerSideParams(params.orderBy().toString(), params, requestParams))
                    .success(function (data, status) {

                        params.total(data.Total);
                        $defer.resolve(data.Results);

                        //记录查询参数，后续返回使用
                        $scope.urlParams = {
                            page: params.page(),
                            Keywords: $scope.Keywords,
                            IsDisplay: $scope.IsDisplay
                        }
                        // $state.go($state.current, urlParams, { location: true, notify: false, reload: false });
                        //$location.search('page', params.page());
                        //$state.transitionTo('app.normalSaleProduct', urlParams, {
                        //    location: true,
                        //    inherit: true,
                        //    relative: $state.$current,
                        //    notify: false
                        //});

                        $scope.isLoading = false;
                    });
            }
        });
        //如果通过链接传值则对参数做一次初始化修改
        $scope.tableParams.page($stateParams.page ? $stateParams.page : 1);
        $scope.Keywords = $stateParams.Keywords ? $stateParams.Keywords : undefined;
        $scope.IsDisplay = $stateParams.IsDisplay ? $stateParams.IsDisplay : undefined;


        //查看商品详情
        $scope.lookProduct = function (sku) {
            //window.open(mallLink + '#/shop/product/?shopno=' + mallKey + "&sku=" + sku);
        }

        //编辑添加
        $scope.updateNormalSaleProduct = function (id) {
            $scope.urlParams.Id = id;
            $state.go('app.exchangeEdit', $scope.urlParams);
            //var url = $state.href('app.updateNormalSaleProductView', { Id: id });
            //window.open(url, '_blank');
        }




        //删除
        $scope.removeNormalSaleProduct = function (index, normalSaleProduct) {
            ngDialog.openConfirm({
                template: 'modalDialogId',
                className: 'ngdialog-theme-default'
            }).then(function (value) {
                $http.post("/ProductExchange/DelExchange", { Id: normalSaleProduct.Id }).then(function (returnData) {
                    if (returnData.data.isSuccess) {
                        $scope.tableParams.data.splice(index, 1);
                        Notify.alert('操作成功!', { status: 'success', timeout: 1000 });
                    } else {
                        Notify.alert(returnData.data.msg, { status: 'warning', timeout: 1000 });
                    }
                });
            }, function (reason) {
            }
            );
        }

        //上下架设置
        $scope.ChangeDisplay = function (normalSaleProduct) {
            ngDialog.openConfirm({
                template: 'confirmChangeStateDialogId',
                className: 'ngdialog-theme-default'
            }).then(function (value) {
                $http.post("/ProductExchange/ChangeDisplay", { Id: normalSaleProduct.Id }).then(function (returnData) {
                    if (returnData.data.isSuccess) {
                        Notify.alert('操作成功!', { status: 'success', timeout: 1000 });
                        normalSaleProduct.IsDisplay = !normalSaleProduct.IsDisplay;
                    } else {
                        Notify.alert(returnData.data.msg, { status: 'warning', timeout: 1000 });
                    }
                });
            }, function (reason) {
            }
            );
        }


    }]);


</script>